package zl.com.gz_2017029e;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import zl.com.gz_2017029e.adapter.AccountAdapter;
import zl.com.gz_2017029e.adapter.ChongZhi;

/**
 * Created by Administrator on 2019/1/8 0008.
 */

public class TopUpDialog extends Dialog implements View.OnClickListener{
    private TextView plateTV;
    private EditText amountET;
    private Button topUpBtn;
    private Button cancelBtn;
    private String plate;
    private int amount;
    private List<ChongZhi>chongZhiList = new ArrayList<>();
    private String plates="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mydialog);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);
        plateTV = (TextView)findViewById(R.id.dialog_plate_tv);
        amountET = (EditText)findViewById(R.id.dialog_topupamount_ed);
        topUpBtn = (Button)findViewById(R.id.dialog_topup_btn);
        cancelBtn = (Button)findViewById(R.id.dialog_cancel);
        topUpBtn.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);
        if(chongZhiList.size() != 0){
            for (ChongZhi element:chongZhiList){
                plates+=element.getPlate()+" ";
            }
            plateTV.setText(plates);
        }else{
            plateTV.setText(plate);
        }

    }

    public TopUpDialog(@NonNull Context context, List<ChongZhi> chongZhiList) {
        super(context,R.style.MyDialog);
        this.chongZhiList=chongZhiList;
    }

    @Override
    public void show() {
        super.show();
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.CENTER;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(layoutParams);
    }

    public String getPlate() {
        return plate;
    }

    public void setPlate(String plate) {
        this.plate = plate;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.dialog_topup_btn:
                if (!amountET.getText().toString().equals("")){
                    final int jine =Integer.parseInt(amountET.getText().toString().trim());
                    if(jine>0 && jine<999){
                        setAmount(jine);
                        cancel();
                        Toast.makeText(getContext(),"充值"+jine+"元",Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(getContext(),"只能充值0到999元之间的金额！",Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(getContext(),"请输入要充值的金额！",Toast.LENGTH_SHORT).show();
                }
                for(ChongZhi cz: chongZhiList){
                    try {
                        URL url = new URL("http://192.168.1.102:8080/transportservice/type"+cz.getPlate());
//                        Log.d("Topuptag", "onClick: "+url);
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case R.id.dialog_cancel:
                cancel();
                break;
                default:break;
        }
    }

    private void sendRequestWithHttpURLConnection(final URL chongZhiUrl){
        //开启线程发送网络请求
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                BufferedReader reader = null;
                try {
                    URL url=chongZhiUrl;
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(8000);
                    connection.setReadTimeout(8000);
                    InputStream in = connection.getInputStream();
                    //对获取到的输入流进行读取
                    reader = new BufferedReader(new InputStreamReader(in));
                    StringBuilder response = new StringBuilder();
                    String line;
                    while ((line=reader.readLine()) != null){
                        response.append(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    if(reader != null){
                        try {
                            reader.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if(connection != null){
                        connection.disconnect();
                    }
                }
            }
        }).start();
    }


}
